package org.nextive.android.weightapp;

import org.nextive.android.weightapp.db.WeightDAO;
import org.nextive.android.weightapp.entities.Weight;
import org.nextive.android.weightapp.utils.WeightCursorAdapter;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;

public class WeightHistory extends ListActivity 
{
	private Button buttonShareWeightInfo;
	
	private WeightDAO weightDAO;
	
	public static final int MENU_DELETE = Menu.FIRST;
	public static final int MENU_EDIT = MENU_DELETE+1;
	
	public static final String EXTRA_KEY_WEIGHT_ID = "id";
	public static final String EXTRA_KEY_WEIGHT_DATE = "date";
	public static final String EXTRA_KEY_WEIGHT_WEIGHT = "weight";
	
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weight_history);
		
		buttonShareWeightInfo = (Button) findViewById(R.id.buttonShareWeightInfo);
		
		weightDAO = new WeightDAO(this);
		weightDAO.open();
		
		fillData();
		
		registerForContextMenu(getListView());
		
		buttonShareWeightInfo.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				Intent ShareWeightInfoIntent = new Intent(WeightHistory.this, ShareWeightInfo.class);
				startActivity(ShareWeightInfoIntent);
			}
		});
	}
	
	public void fillData()
	{
		Cursor cursor = weightDAO.findAll();
		startManagingCursor(cursor);
		WeightCursorAdapter weightCursorAdapter = new WeightCursorAdapter(this, cursor);
		setListAdapter(weightCursorAdapter);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
	{
		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		Weight weigt = (Weight) getListAdapter().getItem(info.position);
		menu.setHeaderTitle(weigt.getDate());
		menu.add(0, MENU_DELETE, 0, R.string.delete);
		menu.add(0, MENU_EDIT, 1, R.string.edit);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) 
	{
		AdapterContextMenuInfo adapter = (AdapterContextMenuInfo) item.getMenuInfo();
		Weight weight = (Weight) getListAdapter().getItem(adapter.position);
		
		switch (item.getItemId())
		{
			case MENU_DELETE:		
				deleteWeight(weight.getId());
				return true;
			case MENU_EDIT:
				Intent intentEditWeight = new Intent(this, LogWeight.class);
				intentEditWeight.putExtra(EXTRA_KEY_WEIGHT_ID, weight.getId());
				intentEditWeight.putExtra(EXTRA_KEY_WEIGHT_DATE, weight.getDate());
				intentEditWeight.putExtra(EXTRA_KEY_WEIGHT_WEIGHT, weight.getWeight());
				startActivity(intentEditWeight);
				return true;
		}
		return super.onContextItemSelected(item);
	}
	
	private void deleteWeight(final long weightId)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(getResources().getString(R.string.sure_delete_selected_weight));
		builder.setCancelable(false);
		builder.setPositiveButton(getResources().getString(R.string.yes), new DialogInterface.OnClickListener() 
		       {
		           public void onClick(DialogInterface dialog, int id) 
		           {   
		        	   weightDAO.delete(weightId);
		        	   fillData();
		           }
		       });
		builder.setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() 
		       {
		           public void onClick(DialogInterface dialog, int id) 
		           {
		                dialog.cancel();
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	@Override
	protected void onDestroy() 
	{
		weightDAO.close();
		super.onDestroy();
	}
}
